package main

import (
	"fmt"
	"github.com/google/uuid"
	"github.com/xuri/excelize/v2"
	"io/ioutil"
	"iot-base/common/dbconn"
	"math/rand"
	"strings"
	"time"
)

type SB struct {
	Id       string `gorm:"primarykey"`
	Sbbh     string `gorm:"uniqueIndex:sbbh"`
	Sbdz     string
	Kzsbmc   string
	Sbzx     byte
	Sbzt     byte
	Sdzt     byte
	Mzt      byte
	Sbjs     byte
	Gmzt     byte
	Kzsbgdzt byte
	Dy       string
	Txzl     string
	Zhgxsj   time.Time
	By1      string
	By2      string
	By3      string
	By4      string
	By5      string
}

func (pSB *SB) TableName() string {
	return "sbxxb_debug"
}

func CreateSB() {
	dbconn.DBConnection.Exec("")
}

type ErrLog struct {
	Id   string `gorm:"primarykey"`
	Sbbh string
	Gzxx string
	Rzsj time.Time
	By1  string
	By2  string
	By3  string
	By4  string
	By5  string
}

func init() {
	rand.Seed(time.Now().Unix())
	dbconn.DBConnection.AutoMigrate(&SB{})
	dbconn.DBConnection.AutoMigrate(&ErrLog{})
}

var Addr = []string{"新疆/乌鲁木齐市/第三师", "新疆/乌鲁木齐市/第四师", "新疆/乌鲁木齐市/第八师"}

const Lan = 87.605879
const Lat = 43.801318

func Init(pSB *SB, count int) []ErrLog {
	id1, _ := uuid.NewUUID()
	id2, _ := uuid.NewUUID()
	pSB.Id = id1.String()
	pSB.Sbbh = id2.String()
	pSB.Sbdz = Addr[rand.Intn(3)]
	pSB.Kzsbmc = fmt.Sprintf("智能监控箱-%d", count)
	bint := rand.Intn(10)
	fmt.Println("*********", bint)
	pSB.Sbzx = byte(bint % 2)
	pSB.Sbzt = 0
	pSB.Sdzt = 0
	pSB.Mzt = 0
	pSB.Sbjs = 0
	pSB.Gmzt = 0 //byte(rand.Intn(1))
	bint = rand.Intn(10) % 2
	fmt.Println("*********", bint)
	pSB.Kzsbgdzt = byte(bint % 2)
	pSB.Zhgxsj = time.Now()
	var result []ErrLog
	//pSB.Txzl = "OK"
	if pSB.Sbzx == 1 || pSB.Kzsbgdzt == 1 {
		result = make([]ErrLog, 0, 2)
		if pSB.Kzsbgdzt == 1 {
			id3, _ := uuid.NewUUID()
			result = append(result, ErrLog{
				Id:   id3.String(),
				Sbbh: pSB.Sbbh,
				Gzxx: "供电故障",
				Rzsj: time.Unix(time.Now().Unix()-500+int64(rand.Intn(100)), 0),
				//pSB.Txzl = "Fail"
			})
		}
		if pSB.Sbzx == 1 {
			id3, _ := uuid.NewUUID()
			result = append(result, ErrLog{
				Id:   id3.String(),
				Sbbh: pSB.Sbbh,
				Gzxx: "设备网络断开",
				Rzsj: time.Unix(time.Now().Unix()-500+int64(rand.Intn(100)), 0),
			})
		}
	}
	pSB.Dy = fmt.Sprintf("%d", rand.Intn(10)+220)

	pSB.By1 = strings.Split(pSB.Sbdz, "/")[1]
	pSB.By2 = strings.Split(pSB.Sbdz, "/")[2]
	pSB.By3 = fmt.Sprintf("%f", Lan+float64(count)*0.00001)
	pSB.By4 = fmt.Sprintf("%f", Lat+float64(count)*0.0001)

	return result

}

func (pErrLog *ErrLog) TableName() string {
	return "sbrzb_debug"
}

func getmapstr(m map[string]interface{}, name string) string {
	v, _ := m[name]
	if v != nil {
		return v.(string)
	}
	return ""
}

const path = "d:\\box\\service\\svr\\bin\\box\\"

func main() {
	dbconn.DBConnection.Exec("delete from sbrzb_debug")
	dbconn.DBConnection.Exec("delete from sbxxb_debug")
	// "github.com/xuri/excelize/v2"
	f, _ := excelize.OpenFile(path + "data.xlsx")
	rows, err := f.GetRows("Sheet1")
	if err != nil {
		fmt.Println(err)
		return
	}
	i := 0
	for _, row := range rows {
		i++
		if i == 1 {
			continue
		}
		var pSB SB
		id1, _ := uuid.NewUUID()
		//id2, _ := uuid.NewUUID()
		pSB.Id = id1.String()
		pSB.Sbbh = pSB.Id

		pSB.Sbdz = row[1]
		pSB.By1 = row[0]
		pSB.By3 = row[5]
		pSB.By4 = row[6]
		pSB.Zhgxsj = time.Now()
		if row[8] == "good" {
			pSB.Sbzt = 0
		} else {
			pSB.Sbzt = 1
			if row[7] == "1" {
				pSB.Sdzt = 1
				pSB.Kzsbgdzt = 1
			} else {
				pSB.Sbzx = 1
				pSB.Gmzt = 1
			}
		}
		result := make([]ErrLog, 0, 2)
		pSB.Kzsbmc = row[1]

		id3, _ := uuid.NewUUID()
		if pSB.Sbzx == 1 {
			id3, _ := uuid.NewUUID()
			result = append(result, ErrLog{
				Id:   id3.String(),
				Sbbh: pSB.Sbbh,
				Gzxx: "网络告警",
				Rzsj: time.Unix(time.Now().Unix()-500+int64(rand.Intn(100)), 0),
			})
		} else {
			result = append(result, ErrLog{
				Id:   id3.String(),
				Sbbh: pSB.Sbbh,
				Gzxx: "供电告警",
				Rzsj: time.Unix(time.Now().Unix()-500+int64(rand.Intn(100)), 0),
			})
		}
		dbconn.DBConnection.Create(&pSB)
		dbconn.DBConnection.Create(result)

		//pSB.By3 =

	}

	var things []map[string]interface{}

	dbconn.DBConnection.Raw(`select t1.lat,t1.lon,  t1.s_name,t1.uid ,t2.s_name as station_name,ts2.state as state
	from thing_stations ts ,things t2,things t1 
	left join thing_states ts2 on t1.uid = ts2.thing_id
	where t2.uid = ts.station_id and ts.thing_id = t1.uid and t1.model_id = 'm_smartBoxForPolice'`).Find(&things)

	for _, thing := range things {
		var pSB SB
		id2 := getmapstr(thing, "uid")
		pSB.Id = id2
		pSB.Sbbh = id2

		pSB.Sbdz = getmapstr(thing, "s_name")
		pSB.By1 = getmapstr(thing, "")
		pSB.By3 = getmapstr(thing, "lon")
		pSB.By4 = getmapstr(thing, "lat")
		state := thing["state"]
		var staten int64
		if state == nil {
			staten = 0
		} else {
			staten = state.(int64)
		}

		pSB.Zhgxsj = time.Now()
		if staten == 1 {
			pSB.Sbzt = 0
		} else {
			pSB.Sbzt = 1
			pSB.Sbzx = 1
			pSB.Gmzt = 1

		}
		result := make([]ErrLog, 0, 2)
		pSB.Kzsbmc = getmapstr(thing, "s_name")

		id3, _ := uuid.NewUUID()
		if pSB.Sbzx == 1 {
			id3, _ := uuid.NewUUID()
			result = append(result, ErrLog{
				Id:   id3.String(),
				Sbbh: pSB.Sbbh,
				Gzxx: "网络告警",
				Rzsj: time.Unix(time.Now().Unix()-500+int64(rand.Intn(100)), 0),
			})
		} else {
			result = append(result, ErrLog{
				Id:   id3.String(),
				Sbbh: pSB.Sbbh,
				Gzxx: "供电告警",
				Rzsj: time.Unix(time.Now().Unix()-500+int64(rand.Intn(100)), 0),
			})
		}
		dbconn.DBConnection.Create(&pSB)
		dbconn.DBConnection.Create(result)
	}
	fmt.Println("----------OK----------------")

	var count int64
	dbconn.DBConnection.Raw("select count(*) from sbxxb_debug").Find(&count)
	var onlinecount int64
	dbconn.DBConnection.Raw("select count(*) from sbxxb_debug where sbzx=0").Find(&onlinecount)

	data := []byte(fmt.Sprintf("all:%d online:%d ", count, onlinecount))

	// 覆盖式写入
	ioutil.WriteFile(path+"test.txt", data, 0664)

	// Close the spreadsheet.

}
